package webapp.mvc.wechat_handlers;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutNewsMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import webapp.mvc.components.SiteProperties;
import webapp.mvc.controllers.HomeController;
import webapp.mvc.enums.UserGenders;
import webapp.mvc.exceptions.UserException;
import webapp.mvc.models.ShopCategoryEntity;
import webapp.mvc.models.UserEntity;
import webapp.mvc.services.ICategoryService;
import webapp.mvc.services.IUserService;

import java.util.List;
import java.util.Map;

public class WechatCategoriesHandler implements WxMpMessageHandler {
    private static final Logger LOG = LoggerFactory.getLogger(WechatCategoriesHandler.class);

    @Autowired
    private IUserService userService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private SiteProperties siteProperties;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map, WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException {
        String openId = wxMpXmlMessage.getFromUserName();
        UserEntity userEntity = userService.findUserByWechatOpenId(openId);
        if (userEntity == null) {
            try {
                userEntity = userService.createWechatUserBinding(openId, openId, UserGenders.PRIVATE, "");
            } catch (UserException e) {
                LOG.error("create wechat user binding error", e);
                return null;
            }
        }
        List<ShopCategoryEntity> shopCategoryEntities = categoryService.getShopCategories();
//        if (true) {
//            return WxMpXmlOutMessage.TEXT()
//                    .content("content ")
//                    .fromUser(wxMpXmlMessage.getToUserName())
//                    .toUser(wxMpXmlMessage.getFromUserName())
//                    .build();
//        }

        me.chanjar.weixin.mp.bean.outxmlbuilder.NewsBuilder builder = WxMpXmlOutMessage.NEWS()
                .toUser(wxMpXmlMessage.getFromUserName())
                .fromUser(wxMpXmlMessage.getToUserName());
        // FIXME: use once token to auto login
        for (ShopCategoryEntity shopCategoryEntity : shopCategoryEntities) {
            WxMpXmlOutNewsMessage.Item article = new WxMpXmlOutNewsMessage.Item();
            article.setUrl(siteProperties.getHost() + "/" + "?" + HomeController.WECHAT_AUTOLOGIN_KEY + "=" + openId + "#/category/" + shopCategoryEntity.getId());
            article.setPicUrl(siteProperties.getDefaultWechatPictureUrl());
            article.setDescription(shopCategoryEntity.getName());
            article.setTitle(shopCategoryEntity.getName());
            builder.addArticle(article);
        }
        builder.build();
        return builder.build();
    }
}
